<?php

namespace app\admin\model;

use think\Model;


class Centralcontactrecords extends Model
{

    

    

    // 表名
    protected $name = 'centralcontactrecords';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'create_time_text',
        'update_time_text',
        'receive_time_text',
    ];
    

    protected static function init()
    {
        Centralcontactrecords::event('before_insert', function ($record){
            if($record -> centraloffice_id){
//                $record -> centraloffice_name = Centraloffice::get($record -> centraloffice_id) -> name;
                $record -> centraloffice_name = (new Centraloffice()) -> whereIn('id',$record -> centraloffice_id) ->value('GROUP_CONCAT(name)');
                $record -> leader_names = (new \app\admin\model\Admin())->whereIn('id', $record -> leader_ids)->value('GROUP_CONCAT(nickname)');
//                $record -> centralcontact_title = Centralcontact::get($record -> centralcontact_id) -> name;
            }
        });
    }


    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUpdateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getReceiveTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['receive_time']) ? $data['receive_time'] : '');
        return is_numeric($value) ? date("Y-m-d", $value) : $value;
    }

    protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setUpdateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setReceiveTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setLeaderIdsAttr($value){
        return is_array($value) ? implode(',',$value) : $value;
    }

    protected function setCentralofficeIdAttr($value){
        return is_array($value) ? implode(',',$value) : $value;
    }

}
